package com.lhq.admin.controller;

import com.lhq.admin.base.BaseResponse;
import com.lhq.admin.entity.UserInfo;
import com.lhq.admin.service.UserInfoService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin")
public class UserInfoController {

    @Autowired
    private UserInfoService userInfoService;

    /**
     * 查询用户列表
     *
     * @param userInfo
     * @return
     */
    @GetMapping("/users")
    @ApiOperation(notes = "查询用户列表", value = "查询用户列表")
    public BaseResponse<List<UserInfo>> getUsers(UserInfo userInfo) {
        List<UserInfo> users = userInfoService.getUsers(userInfo);
        return new BaseResponse<>(20000, users);
    }

    /**
     * 新增用户
     *
     * @param userInfo
     * @return
     */
    @PostMapping("/user")
    public BaseResponse<Boolean> addUser(@RequestBody UserInfo userInfo) {
        boolean addUser = userInfoService.addUser(userInfo);
        return new BaseResponse<>(addUser ? 20000 : 50000, addUser ? "成功" : "失败");
    }

    /**
     * 更新用户
     *
     * @param userInfo
     * @return
     */
    @PutMapping("/user")
    public BaseResponse<Boolean> updateUser(@RequestBody UserInfo userInfo) {
        boolean updateUser = userInfoService.updateUser(userInfo);
        return new BaseResponse<>(updateUser ? 20000 : 50000, updateUser ? "成功" : "失败");
    }

    /**
     * 删除用户
     *
     * @param id
     * @return
     */
    @DeleteMapping("/user")
    public BaseResponse<Boolean> deleteUser(int id) {
        boolean deleteUser = userInfoService.deleteUser(id);
        return new BaseResponse<>(deleteUser ? 20000 : 50000, deleteUser ? "成功" : "失败");
    }

    /**
     * 查询用户详情
     *
     * @param id
     * @return
     */
    @GetMapping("/user")
    public BaseResponse<UserInfo> getUserInfo(int id) {
        UserInfo userInfo = userInfoService.getUserInfo(id);
        return new BaseResponse<>(20000, userInfo);
    }
}
